package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.entity.AllRun;
import com.woniuxy.entity.DTO.GrabEvent;
import com.woniuxy.entity.ShopOrder;
import com.woniuxy.mapper.OrderRunMapper;
import com.woniuxy.mapper.RunMapper;
import com.woniuxy.service.OrderRunService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

@Service
public class OrderRunServiceImpl extends ServiceImpl<OrderRunMapper, ShopOrder> implements OrderRunService {
    @Autowired
    private RunMapper runMapper;
    @Autowired
    private OrderRunMapper orderRunMapper;

    private static final String ORDER_PREFIX = "ORD";
    @Override
    public void createOrder(GrabEvent event) {
        QueryWrapper<AllRun> wrapper = new QueryWrapper<>();
        wrapper.eq("run_number", event.getTaskId());
        AllRun allRun = runMapper.selectOne(wrapper);
        ShopOrder shopOrder = new ShopOrder();

        shopOrder.setOrderNumber(OrderNumberGenerator());
        shopOrder.setUserId(allRun.getUserId());//买家变成发布订单的人
        shopOrder.setSellerId(event.getUserId());//卖家变成接单的人
        shopOrder.setItemId(allRun.getId());
        shopOrder.setTotalAmount(allRun.getPrice());
        shopOrder.setStatus(1);
        shopOrder.setCreateTime(new Date());
        shopOrder.setUpdateTime(new Date());
        shopOrder.setAddress(allRun.getEndaddress());
        shopOrder.setRunNumber(event.getTaskId());

        orderRunMapper.insert(shopOrder);
        System.out.println("订单"+shopOrder);
    }
    public String OrderNumberGenerator(){
        // 生成UUID并截取部分字符（去掉横杠）
        String uuid = UUID.randomUUID().toString().replace("-", "").substring(0, 12);
        // 组合订单号
        return ORDER_PREFIX + uuid;
    }
}
